package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/10/20 21:25
 **/
public class Day48_4_Num233_CountDigitOne {
    class Solution {
        public int countDigitOne(int n) {
            int ans = 0, digit = 1;
            int high = n / 10, cur = n % 10, low = 0;
            while(high != 0 || cur != 0){
                if(cur == 0) ans += high * digit;
                else if(cur == 1) ans += high * digit + low + 1;
                else ans += (high + 1) * digit;

                low += cur*digit;
                cur = high % 10;
                high = high / 10;
                digit *= 10;
            }
            return ans;
        }
    }
}